package com.wealth.system.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.wealth.member.entity.Member;
import com.wealth.member.service.IMemberService;

/**
 * 会员登录验证，和权限验证，该类不要轻易修改
 * @author chan
 *
 */
public class ShiroMemberDbRealm extends AuthorizingRealm{

	Logger logger = Logger.getLogger(ShiroMemberDbRealm.class);
	
	@Autowired
	private IMemberService memberService;
	
	/**
	 * doGetAuthorizationInfo ： 验证当前Subject（可理解为当前会员）所拥有的权限，且给其授权。
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        String username = (String) getAvailablePrincipal(principals);
		Member member = ShiroMember.getInstance().getMember();
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		
		
		List<String> roles = new ArrayList<String>();
		List<String> premissions = new ArrayList<String>();
		if(member != null){
//			if(user.getRoles()!=null && user.getRoles().size()>0){
//				for (Role role : user.getRoles()) {
//					roles.add(role.getName());
//					if(role.getPermissions() != null && role.getPermissions().size()>0){
//						for (Permission permission : role.getPermissions()) {
//							if(!StringUtils.isEmpty(permission.getPermission())){
//								premissions.add(permission.getPermission());
//							}
//						}
//					}
//				}
//			}
		}else{
			logger.info("会员【"+username +"】不存在！");
			throw new AuthenticationException();
		}
		//设置角色
		info.addRoles(roles);
		//设置权限
		info.addStringPermissions(premissions);
		
		return info;
	}

	/**
     *  认证回调函数,登录时调用.
     *  doGetAuthenticationInfo ： 验证当前Subject登录。
     */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken arg0) throws AuthenticationException {
		logger.info("认证回调函数,登录时调用.");
		
		//根据会员名查询会员信息
		//User user = userService.getUserByUsername((UsernamePasswordToken) arg0);
		UsernamePasswordToken token = (UsernamePasswordToken) arg0;
		Member member = memberService.findMemberByUsername(token.getUsername() );
		
		if(member==null){
			throw new UnknownAccountException();//没找到帐号
		}else{
			return new SimpleAuthenticationInfo(
					member,member.getPassword(),getName());
		}
	}

	
}
